package com.abkj.platform.util;

import java.io.PrintStream;

public class PublicTool
{
  public static byte[] intToBytes(int num)
  {
    byte[] b = new byte[4];
    for (int i = 0; i < 4; i++) {
      b[i] = ((byte)(num >>> 24 - i * 8));
    }
    return b;
  }

  public static long bytes2long(byte[] b) {
    long temp = 0L;
    long res = 0L;
    for (int i = 0; i < 8; i++) {
      res <<= 8;
      temp = b[i] & 0xFF;
      res |= temp;
    }
    return res;
  }

  public static byte[] long2bytes(long num) {
    byte[] b = new byte[8];
    for (int i = 0; i < 8; i++) {
      b[i] = ((byte)(int)(num >>> 56 - i * 8));
    }
    return b;
  }

  public static byte[] shortToBytes(short x) {
    byte[] ar = new byte[2];

    ar[0] = ((byte)(x >> 8));
    ar[1] = ((byte)(x >> 0));

    return ar;
  }

  public static byte byteToBit(byte b) {
    return (byte) ((byte)(b >> 7 & 0x1) + (byte)(b >> 6 & 0x1) + (byte)(b >> 5 & 0x1) + (byte)(b >> 4 & 0x1) + (byte)(b >> 3 & 0x1) + (byte)(b >> 2 & 0x1) + (byte)(b >> 1 & 0x1) + (byte)(b >> 0 & 0x1));
  }

  public static short bytesToShort(byte[] b)
  {
    return (short)(b[1] & 0xFF | 
      (b[0] & 0xFF) << 8);
  }

  public static byte[] getBooleanArray(byte b)
  {
    byte[] array = new byte[8];
    for (int i = 7; i >= 0; i--) {
      array[i] = ((byte)(b & 0x1));
      b = (byte)(b >> 1);
    }
    return array;
  }

  public static void printHexString(byte[] b) {
    for (int i = 0; i < b.length; i++) {
      String hex = Integer.toHexString(b[i] & 0xFF);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }
      System.out.print(hex.toUpperCase());
    }
    System.out.print("\n");
  }

  public static String getHexString(byte[] b, int offset, int len) {
    String hex = "";
    for (int i = 0; i < len; i++) {
      String hexTemp = Integer.toHexString(b[(offset + i)] & 0xFF);
      if (hexTemp.length() == 1) {
        hexTemp = '0' + hexTemp;
      }

      hex = hex + hexTemp.toUpperCase();
    }
    return hex;
  }

  public static String getHexString(byte[] b) {
    String hex = "";
    for (int i = 0; i < b.length; i++) {
      String hexTemp = Integer.toHexString(b[i] & 0xFF);
      if (hexTemp.length() == 1) {
        hexTemp = '0' + hexTemp;
      }

      hex = hex + hexTemp.toUpperCase();
    }
    return hex;
  }

  public static String getHexString(byte b) {
    String hexTemp = Integer.toHexString(b & 0xFF);
    if (hexTemp.length() == 1) {
      hexTemp = '0' + hexTemp;
    }
    return hexTemp;
  }

  public static int bytesToInt(byte[] data, int offset) {
    int num = 0;
    for (int i = offset; i < offset + 4; i++) {
      num <<= 8;
      num |= data[i] & 0xFF;
    }
    return num;
  }

  public static String bcd2Str(byte[] bytes)
  {
    StringBuffer temp = new StringBuffer(bytes.length * 2);
    for (int i = 0; i < bytes.length; i++) {
      temp.append((byte)((bytes[i] & 0xF0) >>> 4));
      temp.append((byte)(bytes[i] & 0xF));
    }
    return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
      .toString().substring(1) : temp.toString();
  }

  public static byte[] str2Bcd(String asc)
  {
    int len = asc.length();
    int mod = len % 2;
    if (mod != 0) {
      asc = "0" + asc;
      len = asc.length();
    }
    byte[] abt = new byte[len];
    if (len >= 2) {
      len /= 2;
    }
    byte[] bbt = new byte[len];
    abt = asc.getBytes();

    for (int p = 0; p < asc.length() / 2; p++)
    {
      int j;
      if ((abt[(2 * p)] >= 48) && (abt[(2 * p)] <= 57)) {
        j = abt[(2 * p)] - 48;
      }
      else
      {
        if ((abt[(2 * p)] >= 97) && (abt[(2 * p)] <= 122))
          j = abt[(2 * p)] - 97 + 10;
        else
          j = abt[(2 * p)] - 65 + 10;
      }
      int k;
      if ((abt[(2 * p + 1)] >= 48) && (abt[(2 * p + 1)] <= 57)) {
        k = abt[(2 * p + 1)] - 48;
      }
      else
      {
        if ((abt[(2 * p + 1)] >= 97) && (abt[(2 * p + 1)] <= 122))
          k = abt[(2 * p + 1)] - 97 + 10;
        else
          k = abt[(2 * p + 1)] - 65 + 10;
      }
      int a = (j << 4) + k;
      byte b = (byte)a;
      bbt[p] = b;
    }
    return bbt;
  }
}